//
//  WYCMessegeController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/2.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCMessegeController.h"
#import "WYCMessegeCell.h"
#import "WYCMessegeModel.h"

@interface WYCMessegeController ()<UITableViewDataSource,UITableViewDelegate>


/** 数组*/
@property(nonatomic,strong)NSMutableArray * dataArray;

//列表创建
@property(nonatomic,strong)UITableView * tableView;

@end

@implementation WYCMessegeController

- (void)viewDidLoad {
    [super viewDidLoad];
    //数组初始化
    self.dataArray = [[NSMutableArray alloc] init];
    
    //NAV按钮及颜色
    [self setupNavButton];
    
    //表视图
    [self setupTableView];
    
    
    //请求
    [self setupRequster];
    
    
    
}
#pragma make ----------->>请求
- (void)setupRequster{
    
    NSString * UrlString = [NSString stringWithFormat:@"%@token=%@",messagelist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:UrlString params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dic = responseObject[@"result"];
            NSArray * dataArray = dic[@"data"];
            for (NSDictionary * dict in dataArray) {
                WYCMessegeModel * messegeModel = [WYCMessegeModel mj_objectWithKeyValues:dict];
                [self.dataArray addObject:messegeModel];
            }
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        if (self.dataArray.count == 0) {
            self.tableView.hidden = YES;
            [self ObtainButton];
        }else{
            self.tableView.hidden = NO;
            
        }
        [self.tableView reloadData];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)ObtainButton{
    
    UILabel * label = [[UILabel alloc] init];
    label.text = @"暂无消息";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:MiddleFont];
    label.frame = CGRectMake(0, 800 * px, ScreenWidth, 60);
    label.textColor = WYCRGBColor(200, 200, 200);
    [self.view  addSubview:label];
}
//表视图
- (void)setupTableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.scrollIndicatorInsets = _tableView.contentInset;
    
    CGFloat tableviewY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableviewY, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = WYCGlobalBg;
    
    [self.view addSubview:self.tableView];
    
}
//Nav按钮及颜色
- (void)setupNavButton{
    self.navigationItem.title = @"消息中心";
    self.view.backgroundColor = WYCGlobalBg;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBarTintColor:WYCMain];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 250 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WYCMessegeCell * cell = [WYCMessegeCell cellWithTableView:tableView];
    cell.messegeModel = self.dataArray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    WYCMessegeModel * model = self.dataArray[indexPath.row];
    //        WYCMSdetailedController * Mscontroller = [[WYCMSdetailedController alloc] init];
    //        Mscontroller.content = model.content;
    //
    //        [self.navigationController pushViewController:Mscontroller animated:NO];
    //
    //        // 取消选中状态
    //        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"消息中心";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
